﻿using ProjScan.Tools;
using System.Collections.Generic;
using System.Text;
using UnityEditor;
using UnityEngine;

namespace ProjScan.RecordManager.AssetData
{
    internal class SerializedObjectAssetData : ReferencedAssetData
    {
        public bool isValid;

        public SerializedObjectAssetData(int assetId, Component component, List<string> referencedAssets)
            : base(assetId, component, referencedAssets)
        {
            isValid = true;
            if (component == null)
            {
                isValid = false;
            }

            SerializedProperty iterator = new SerializedObject(component).GetIterator();
            while (iterator.NextVisible(enterChildren: true))
            {
                if (iterator.propertyType == SerializedPropertyType.String && iterator.objectReferenceValue == null && iterator.objectReferenceInstanceIDValue != 0)
                {
                    isValid = false;
                    break;
                }
            }
        }

        public override string GetCSVHeader()
        {
            return "Name,DirID,NumOfReferencedAsset";
        }

        public override string GetInfo()
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append(ToolsHelper.QuoteString(base.AssetName));
            stringBuilder.Append(',');
            stringBuilder.Append(base.DirectoryId);
            stringBuilder.Append(',');
            stringBuilder.Append(base.ReferenceList.Count);
            return stringBuilder.ToString();
        }
    }
}